package com.alibaba.middleware.hsf.guide.serverfilter;


import com.taobao.hsf.annotation.Order;
import com.taobao.hsf.invocation.Invocation;
import com.taobao.hsf.invocation.InvocationHandler;
import com.taobao.hsf.invocation.RPCResult;
import com.taobao.hsf.invocation.filter.ServerFilter;
import com.taobao.hsf.util.concurrent.ListenableFuture;

/**
 * Created by sixie.xyn on 2016/1/15.
 */
@Order(300)
public class HSFServerFilter implements ServerFilter {
    public ListenableFuture<RPCResult> invoke(InvocationHandler invocationHandler,
                                              Invocation invocation) throws Throwable {
        //process args
        String[] sigs = invocation.getMethodArgSigs();
        Object[] args = invocation.getMethodArgs();

        System.out.println("#### intercept request");
        System.out.println("args type is :");
        for (String sig : sigs) {
            System.out.print(sig);
            System.out.print(";");
        }

        System.out.println("args value is :");
        for (Object arg : args) {
            System.out.print(arg);
            System.out.print(";");
        }
        System.out.println();
        return invocationHandler.invoke(invocation);
    }

    public void onResponse(Invocation invocation, RPCResult rpcResult) {
        System.out.println("#### intercept response");
        Object resp = rpcResult.getAppResponse();
        System.out.println(resp);
    }
}
